package com.primecodegen.common.DataModelAttributes;

import com.primecodegen.common.DataModelObject.DataModelObjectDAO;
import com.primecodegen.common.DataModelObject.DataModelObjectTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "dataModelAttributesBean")
@RequestScoped

public class DataModelAttributesBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(DataModelAttributesBean.class.getName());
    private int count;
    private int dataModelAttributesID;
    private String excludedClmn;
    private DataModelAttributesTable selectedDataModelAttributes;
    HttpSession session;
    private DataModelAttributesDAO dataModelAttributesDAO;
    private ArrayList<DataModelAttributesTable> dataModelAttributesList;
    private DataModelAttributesTable dataModelAttributesSrchCrit;
    private DataModelAttributesTable dataModelAttributesUpdate;
    private DataModelAttributesTable dataModelAttributesTable;
    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public DataModelAttributesBean() {
        logger.info(" Constructor DataModelAttributesBean()");
        excludedClmn = "6";
        context = getContext();
        session = getSession();
        dataModelAttributesDAO = new DataModelAttributesDAO();
        dataModelAttributesList = new ArrayList<DataModelAttributesTable>();
        dataModelAttributesTable = new DataModelAttributesTable();
        dataModelAttributesSrchCrit = (DataModelAttributesTable) session.getAttribute("sessionDataModelAttributesSrchCrit");
        dataModelAttributesUpdate = (DataModelAttributesTable) session.getAttribute("dataModelAttributesUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (dataModelAttributesSrchCrit == null) {
            dataModelAttributesSrchCrit = new DataModelAttributesTable();
        }

        if (dataModelAttributesUpdate == null) {
            dataModelAttributesUpdate = new DataModelAttributesTable();
        }



        fillDataModelAttributesList();
    }

    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public DataModelAttributesTable getDataModelAttributesUpdate() {
        dataModelAttributesUpdate = (DataModelAttributesTable) session.getAttribute("dataModelAttributesUpdateSess");
        if (dataModelAttributesUpdate == null) {
            dataModelAttributesUpdate = new DataModelAttributesTable();
        }
        return dataModelAttributesUpdate;
    }

    public void setDataModelAttributesUpdate(DataModelAttributesTable dataModelAttributesUpdate) {
        session.setAttribute("dataModelAttributesUpdateSess", dataModelAttributesUpdate);
        this.dataModelAttributesUpdate = dataModelAttributesUpdate;
    }

    public DataModelAttributesBean(DataModelAttributesTable dataModelAttributesTable) {
        logger.info("Constructor DataModelAttributesBean( DataModelAttributesTable dataModelAttributesTable )");
        this.dataModelAttributesTable = dataModelAttributesTable;
    }

    public DataModelAttributesTable getDataModelAttributesTable() {
        return dataModelAttributesTable;
    }

    public void setDataModelAttributesTable(DataModelAttributesTable dataModelAttributesTable) {
        this.dataModelAttributesTable = dataModelAttributesTable;
    }

    public DataModelAttributesTable getDataModelAttributesSrchCrit() {
        return dataModelAttributesSrchCrit;
    }

    public void setDataModelAttributesSrchCrit(DataModelAttributesTable dataModelAttributesSrchCrit) {
        this.dataModelAttributesSrchCrit = dataModelAttributesSrchCrit;
    }

    public DataModelAttributesTable getSelectedDataModelAttributes() {
        return selectedDataModelAttributes;

    }

    public void setSelectedDataModelAttributes(DataModelAttributesTable selectedDataModelAttributes) {
        this.selectedDataModelAttributes = selectedDataModelAttributes;
    }

    public ArrayList<DataModelAttributesTable> getDataModelAttributesList() {
        return dataModelAttributesList;
    }
//******************************
    private ArrayList<SelectItem> dataModelObjectList = new ArrayList<SelectItem>();
    private DataModelObjectDAO dataModelObjectDAO = new DataModelObjectDAO();

    public ArrayList<SelectItem> getDataModelObjectList() {
        getAllDataModelObject();
        return dataModelObjectList;
    }

    public void setDataModelObjectList(ArrayList<SelectItem> dataModelObjectList) {
        this.dataModelObjectList = dataModelObjectList;
    }

    public void getAllDataModelObject() {
        dataModelObjectList = new ArrayList<SelectItem>();
        DataModelObjectTable groupTable = new DataModelObjectTable();
        dataModelObjectList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itDataModelObject = dataModelObjectDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itDataModelObject.hasNext()) {
                groupTable = (DataModelObjectTable) itDataModelObject.next();
                dataModelObjectList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new DataModelObjectTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllDataModelObject() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in DataModelAttributesBean");
        try {
            dataModelAttributesSrchCrit.setFilter(true);
            this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(dataModelAttributesSrchCrit, null);

            session.setAttribute("sessionDataModelAttributesSrchCrit", dataModelAttributesSrchCrit);
            fillDataModelAttributesList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in DataModelAttributesBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

    public void clearFilter() {
        try {
            dataModelAttributesSrchCrit = null;
            session.setAttribute("sessionDataModelAttributesSrchCrit", dataModelAttributesSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillDataModelAttributesList ()");

        try {

            if (dataModelAttributesSrchCrit != null && colToOrderBy != null) {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(dataModelAttributesSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dataModelAttributesSrchCrit != null) {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(dataModelAttributesSrchCrit, null);
            } else {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDataModelAttributesList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("dataModelAttributesIDSess").toString());
            dataModelAttributesUpdate.setID(parID);
            dataModelAttributesDAO.updateByID(dataModelAttributesUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = dataModelAttributesDAO.insert(dataModelAttributesTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "DataModelAttributes Added"));
                fillDataModelAttributesList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in dataModelAttributesDAO.insert( dataModelAttributesTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("dataModelAttributesIDSess").toString());



            dataModelAttributesDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "DataModelAttributes Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("dataModelAttributesID"));
            dataModelAttributesUpdate = (DataModelAttributesTable) dataModelAttributesDAO.getByID(parID);
            session.setAttribute("dataModelAttributesUpdateSess", dataModelAttributesUpdate);
            session.setAttribute("dataModelAttributesIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(DataModelAttributesBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillDataModelAttributesList() {
        logger.log(Level.INFO, "Starting: public void fillDataModelAttributesList () in DataModelAttributesBean");
        try {

            if ((dataModelAttributesSrchCrit != null && dataModelAttributesSrchCrit.isFilter()) && colToOrderBy != null) {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(dataModelAttributesSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dataModelAttributesSrchCrit != null && dataModelAttributesSrchCrit.isFilter()) {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(dataModelAttributesSrchCrit, null);
            } else {
                this.dataModelAttributesList = dataModelAttributesDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillDataModelAttributesList () in DataModelAttributesBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDataModelAttributesList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
}
